package com.hongyang.servlet.baby;

import com.hongyang.entity.Baby;
import com.hongyang.entity.Hospital;
import com.hongyang.service.BabyService;
import com.hongyang.service.HospitalService;
import com.hongyang.service.impl.BabyServiceImpl;
import com.hongyang.service.impl.HospitalServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@WebServlet(urlPatterns = "/hr/newbaby/updatebabydangan")
public class Update extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String babyId = req.getParameter("id");
        BabyService service=new BabyServiceImpl();
        Baby baby = service.selectByPrimaryKey(babyId);

        HospitalService hospitalService=new HospitalServiceImpl();
        List<Hospital> hospitalList = hospitalService.search();

        req.setAttribute("hospitalList",hospitalList);
        req.setAttribute("baby",baby);
        req.getRequestDispatcher("/WEB-INF/baby/update.jsp").forward(req,res);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //设置编码格式
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat sdfTime=new SimpleDateFormat("HH:mm");

        Baby baby=new Baby();
        //出生证号
        String birthNum=req.getParameter("birthNum");
        baby.setBirthNum(birthNum);
        //婴儿姓名
        String babyName=req.getParameter("babyName");
        baby.setBabyName(babyName);
        //性别 男：1  女：0 不详：2
        Integer babySex=Integer.parseInt(req.getParameter("babySex"));
        baby.setBabySex(babySex);
        //住院号
        String admissionId=req.getParameter("admissionId");
        baby.setAdmissionId(admissionId);
        //住院日期
        try {
            Date admissionDate=sdf.parse(req.getParameter("admissionDate"));
            baby.setAdmissionDate(admissionDate);
            //出院日期
            Date dischargeDate=sdf.parse(req.getParameter("dischargeDate"));
            baby.setDischargeDate(dischargeDate);
            //出生日期
            Date birthDate=sdf.parse(req.getParameter("birthDate"));
            baby.setBirthDate(birthDate);
            //出生时间
            Date birthTime=sdfTime.parse(req.getParameter("birthTime"));
            baby.setBirthTime(birthTime);
            //出生孕周
            Integer birthWeek=Integer.parseInt(req.getParameter("birthWeek"));
            baby.setBirthWeek(birthWeek);
            //出生孕天
            Integer birthDay=Integer.parseInt(req.getParameter("birthDay"));
            baby.setBirthDay(birthDay);
            //分娩地点 家中: 0  医院: 1  其他: 2
            Integer deliveryAddress=Integer.parseInt(req.getParameter("deliveryAddress"));
            baby.setDeliveryAddress(deliveryAddress);
            //接生医院 福建省妇幼保健院:JSJG001     福建省立医院JSJG002
            String deliveryHospital=req.getParameter("deliveryHospital");
            baby.setDeliveryHospital(deliveryHospital);
            //接生员  李春燕:JSY1001      王晓敏:JSY1002  吴喜:  JSY2001     张霞:JSY2002
            String midwifeId=req.getParameter("midwifeId");
            baby.setMidwifeId(midwifeId);
            //胎数  单胎:1      双胎:2      多胎:3
            Integer babyNum=Integer.parseInt(req.getParameter("babyNum"));
            baby.setBabyNum(babyNum);
            //胎次
            Integer deliveryNum=Integer.parseInt(req.getParameter("deliveryNum"));
            baby.setDeliveryNum(deliveryNum);
            //体重
            Float birthWeight=Float.parseFloat(req.getParameter("birthWeight"));
            baby.setBirthWeight(birthWeight);
            //身长
            Integer birthLength=Integer.parseInt(req.getParameter("birthLength"));
            baby.setBirthLength(birthLength);
            //编号
            String babyId=req.getParameter("babyId");
            baby.setBabyId(babyId);
            //是否删除 0未删除，1已删除
            baby.setIsDelete(0);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        BabyService service=new BabyServiceImpl();
        boolean success = service.updateByPrimaryKey(baby);

        if(success){
            //跳转到查询servlet
            String project= req.getContextPath();
            res.sendRedirect(project+"/show");
        }
    }
}
